/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.wizards.htmlexport;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.toedo.gui.translations.Messages;

public class HtmlExportIntroPage extends WizardPage {

	private Composite mContainer;
	
	private Text mFileNameText;
	private Button mIncludeToc;
	private Button mIncludeClosedTasks;
	
	public HtmlExportIntroPage() {
		super("HtmlExportIntroPage"); //$NON-NLS-1$
		setTitle(Messages.HtmlExportIntroPage_WizardTitle);
		setDescription(Messages.HtmlExportIntroPage_IntroPageDescription);
	}

	@Override
	public void createControl(final Composite parent) {
		mContainer = new Composite(parent, SWT.NULL);
		
		GridLayout gl = new GridLayout(3, false);
		mContainer.setLayout(gl);
		
		Label fileNameLabel = new Label(mContainer, SWT.NONE);
		fileNameLabel.setText(Messages.HtmlExportIntroPage_OutputFile);
		
		mFileNameText = new Text(mContainer, SWT.BORDER);
		mFileNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		mFileNameText.setEnabled(false);
		
		Button fileNameBrowse = new Button(mContainer, SWT.PUSH);
		fileNameBrowse.setText(Messages.BrowseButton);
		fileNameBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog(parent.getShell(), SWT.SAVE);
			    dialog.setFilterExtensions(new String[] { "*.html", "*.*" }); //$NON-NLS-1$ //$NON-NLS-2$
			    dialog.setFilterNames(new String[] { Messages.HTMLFilesFilter, Messages.AllFilesFilter });
			    dialog.setOverwrite(true);
			    
			    String fileSelected = dialog.open();
			    
			    if ((fileSelected != null) &&
			    		(!fileSelected.isEmpty())) {
			    	mFileNameText.setText(fileSelected);
			    	setPageComplete(internalIsPageComplete());
			    }
			}
		});
		
		mIncludeToc = new Button(mContainer, SWT.CHECK);
		mIncludeToc.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 3, 1));
		mIncludeToc.setText(Messages.HtmlGenerator_IncludeToc);
		mIncludeToc.setSelection(true);
		
		mIncludeClosedTasks = new Button(mContainer, SWT.CHECK);
		mIncludeClosedTasks.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false, 3, 1));
		mIncludeClosedTasks.setText(Messages.HtmlGenerator_IncludeClosedTasks);
	
		// Required to avoid an error in the system
		setControl(mContainer);
		setPageComplete(internalIsPageComplete());
	}
	
	private boolean internalIsPageComplete() {
		return (mFileNameText.getText() != null) && (!mFileNameText.getText().isEmpty());
	}
	
	public String getFileName() {
		return mFileNameText.getText();
	}
	
	public boolean getIncludeToc() {
		return mIncludeToc.getSelection();
	}
	
	public boolean getIncludeClosedTasks() {
		return mIncludeClosedTasks.getSelection();
	}
}
